import unittest
from config.settings import get_config
from utils.browser import BrowserManager
from utils.logger import main_logger
from pages.pages_S01.mock_server_pages import AdvancedMockServer

class BaseTestCase(unittest.TestCase):
    """自动继承全局设备配置的基类"""

    # 默认使用全局配置，子类可覆盖
    device_type = None

    @classmethod
    def setUpClass(cls):
        # 设备类型有效性检查
        if not hasattr(cls, 'device_type') or not cls.device_type:
            raise ValueError("测试类必须定义 device_type 属性或继承默认配置")

        cls.mock_server = AdvancedMockServer()
        # 动态配置初始化
        cls.config = get_config(cls.device_type)
        cls.logger = main_logger(cls.device_type)

        # 浏览器初始化
        cls.driver = BrowserManager.get_driver(cls.device_type)
        cls.driver.get(cls.config.base_url)
        # 初始化页面对象
        cls._init_pages()
        # 启动模拟服务器
        cls.mock_server.start()
        cls.logger.info(f"【{cls.__name__}】测试类初始化完成")

    @classmethod
    def _init_pages(cls):
        """初始化页面对象（需被子类实现）"""
        raise NotImplementedError("子类必须实现 _init_pages 方法")

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()
        # 停止模拟服务器
        cls.mock_server.stop()
        cls.logger.info(f"【{cls.__name__}】测试环境已清理")